package com.eclub.emotion.rootstone.server.context;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.eclub.emotion.rootstone.server.IFilter;
import com.eclub.emotion.rootstone.server.Server;
import com.eclub.emotion.rootstone.server.ServerStateType;

/**
 * 
 * Function: TODO ADD FUNCTION. <br/> 
 * @author jun.yang
 *
 */
public class Global {
	private ServiceConfig serviceConfig = null;
	
	private IProxyFactory proxyFactory = null;
	
	private List<Server> serverList = new ArrayList<Server>();
	
	private List<IFilter> globalRequestFilterList = new ArrayList<IFilter>();
	private List<IFilter> globalResponseFilterList = new ArrayList<IFilter>();
	private List<IFilter> connectionFilterList = new ArrayList<IFilter>();
	
	
	private String rootPath;
	
	private ThreadLocal<RsContext> threadLocal = new ThreadLocal<RsContext>();
	
	private static final Object lockHelper = new Object();
	
	private Map<String,List<String>> secureMap = new HashMap<String,List<String>>();
	
	/**
	 * Server Running Status
	 */
	private ServerStateType serverState = ServerStateType.Normal;
	
	static class SingletonHolder{
		static volatile Global instance = new Global();   
	}
	
	public static Global getSingleton() {
		return SingletonHolder.instance;
	}
	
	private Global() {
		
	}
	
	public RsContext getRsContext(){
		return getThreadLocal().get();
	}
	
	public ServiceConfig getServiceConfig() {
		return this.serviceConfig;
	}

	public void setServiceConfig(ServiceConfig serviceConfig) {
		this.serviceConfig = serviceConfig;
	}

	public List<IFilter> getGlobalRequestFilterList() {
		return this.globalRequestFilterList;
	}

	public List<IFilter> getGlobalResponseFilterList() {
		return this.globalResponseFilterList;
	}
	
	public List<IFilter> getConnectionFilterList() {
		return connectionFilterList;
	}

	public void setConnectionFilterList(List<IFilter> connectionFilterList) {
		this.connectionFilterList = connectionFilterList;
	}

	public void setProxyFactory(IProxyFactory proxyFactory) {
		this.proxyFactory = proxyFactory;
	}

	public IProxyFactory getProxyFactory() {
		return this.proxyFactory;
	}

	public List<Server> getServerList() {
		return this.serverList;
	}
	
	public void setRootPath(String rootPath) {
		this.rootPath = rootPath;
	}

	public String getRootPath() {
		return this.rootPath;
	}

	public void addServer(Server server) {
		synchronized (serverList) {
			serverList.add(server);
		}
	}
	
	public void addGlobalRequestFilter(IFilter filter) {
		synchronized (globalRequestFilterList) {
			globalRequestFilterList.add(filter);
		}
	}
	
	public void removeGlobalRequestFilter(IFilter filter) {
		synchronized (globalRequestFilterList) {
			globalRequestFilterList.remove(filter);
		}
	}
	
	public void addGlobalResponseFilter(IFilter filter) {
		synchronized (globalResponseFilterList) {
			globalResponseFilterList.add(filter);
		}
	}
	
	public void removeGlobalResponseFilter(IFilter filter) {
		synchronized (globalResponseFilterList) {
			globalResponseFilterList.remove(filter);
		}
	}
	
	
	public void addConnectionFilter(IFilter filter) {
		synchronized (connectionFilterList) {
			connectionFilterList.add(filter);
		}
	}

	public void removeConnectionFilter(IFilter filter) {
		synchronized (connectionFilterList) {
			connectionFilterList.remove(filter);
		}
	}

	public ThreadLocal<RsContext> getThreadLocal() {
		return threadLocal;
	}
	
	
	public ServerStateType getServerState() {
		return serverState;
	}

	public void setServerState(ServerStateType serverState) {
		this.serverState = serverState;
	}
	
}
